package com.wennimei.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.springframework.stereotype.Service;

import com.wennimei.domain.Parameters;
import com.wennimei.domain.Soft;
import com.wennimei.enums.SolrTypeEnum;

@SuppressWarnings("deprecation")
@Service
public class SolrService {

    private static Logger log = Logger.getLogger(SolrService.class);

    private SolrServer server;

    private static final String solrUrl = "http://10.129.22.197:8983/solr/";

    public String getSolrUrl(SolrTypeEnum type) {
        return solrUrl + type.getType();
    }

    private void doIndex(SolrTypeEnum type, SolrInputDocument doc) {
        try {
            server = new CommonsHttpSolrServer(getSolrUrl(type));
            server.add(doc);
            server.commit();
        } catch (Exception e) {
            log.error("index error", e);
        }
    }

    private SolrDocumentList search(SolrTypeEnum type, String fieldName,
            String keyword, int start, int rows) {
        try {
            server = new CommonsHttpSolrServer(getSolrUrl(type));
            SolrQuery query = new SolrQuery();
            query.setQuery((new StringBuilder()).append(fieldName + ":")
                    .append(keyword).toString());
            query.setStart(start);
            query.setRows(rows);
            QueryResponse resp = server.query(query);
            SolrDocumentList docs = resp.getResults();
            return docs;
        } catch (Exception e) {
            log.error("search error", e);
        }
        return null;
    }

    private SolrDocumentList search(SolrTypeEnum type, String q, int start,
            int rows) {
        try {
            server = new CommonsHttpSolrServer(getSolrUrl(type));
            SolrQuery query = new SolrQuery();
            query.setQuery(q);
            query.setStart(start);
            query.setRows(rows);
            QueryResponse resp = server.query(query);
            SolrDocumentList docs = resp.getResults();
            return docs;
        } catch (Exception e) {
            log.error("search error", e);
        }
        return null;
    }

    public void doParametersIndex(Integer id, String title, String parameters,
            String picUrl) {
        log.info("start do index " + id);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", id);
        doc.addField("title", title);
        doc.addField("parameters", parameters);
        doc.addField("picUrl", picUrl);
        doIndex(SolrTypeEnum.PARAMETERS, doc);
        log.info("do index " + id + " success!");
    }

    public void doQAIndex(Long id, String title) {
        log.info("start do index " + id);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", id);
        doc.addField("title", title);
        doIndex(SolrTypeEnum.QA, doc);
        log.info("do index " + id + " success!");
    }

    public void doSoftIndex(Integer id, String title, String picUrl, String info) {
        log.info("start do index " + id);
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", id);
        doc.addField("title", title);
        doc.addField("picUrl", picUrl);
        doc.addField("info", info);
        doIndex(SolrTypeEnum.SOFT, doc);
        log.info("do index " + id + " success!");
    }

    public List<Parameters> searchParameters(String keyword) {
        SolrDocumentList docs = search(SolrTypeEnum.PARAMETERS, "title",
                keyword, 0, 3);
        List<Parameters> result = new ArrayList<Parameters>();
        Iterator<SolrDocument> it = docs.iterator();
        while (it.hasNext()) {
            try {
                SolrDocument doc = it.next();
                Parameters p = new Parameters((Integer) doc.get("id"),
                        (String) doc.get("title"),
                        (String) doc.get("parameters"),
                        (String) doc.get("picUrl"));
                result.add(p);
            } catch (Exception e) {
                continue;
            }
        }
        return result;
    }

    public List<Soft> searchSoft(String keyword) {
        SolrDocumentList docs = search(SolrTypeEnum.SOFT, "title:" + keyword
                + " OR info:" + keyword, 0, 5);
        List<Soft> result = new ArrayList<Soft>();
        Iterator<SolrDocument> it = docs.iterator();
        while (it.hasNext()) {
            try {
                SolrDocument doc = it.next();
                Soft p = new Soft((Integer) doc.get("id"),
                        (String) doc.get("title"), (String) doc.get("picUrl"),
                        (String) doc.get("info"));
                result.add(p);
            } catch (Exception e) {
                continue;
            }
        }
        return result;
    }

    public List<Long> searchQA(String keyword) {
        SolrDocumentList docs = search(SolrTypeEnum.QA, "title:" + keyword, 0,
                3);
        List<Long> result = new ArrayList<Long>();
        Iterator<SolrDocument> it = docs.iterator();
        while (it.hasNext()) {
            try {
                SolrDocument doc = it.next();
                result.add((Long) doc.get("id"));
            } catch (Exception e) {
                continue;
            }
        }
        return result;
    }
}
